//
//  DHS2_ProductDetailController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/9.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_ProductDetailController.h"
#import "MessageButton.h"
#import "DHProductDetail_HeadCell.h"
#import "DHProductDetail_DianpingCell.h"
#import "DHProductDetail_ProductCell.h"
#import "DHShoppingCart_PushController.h"

@interface DHS2_ProductDetailController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (strong, nonatomic) MessageButton *cartButton;
@property (strong, nonatomic) NSDictionary *productDic;
@property (strong, nonatomic) NSArray *sizesArray;
@property (strong, nonatomic) NSArray *dianpingArray;
@property (strong, nonatomic) NSArray *relatedArrray;

@end

@implementation DHS2_ProductDetailController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (NSArray *)sizesArray{
    if (!_sizesArray) {
        _sizesArray = [NSArray array];
    }
    return _sizesArray;
}

- (NSArray *)dianpingArray{
    if (!_dianpingArray) {
        _dianpingArray = [NSArray array];
    }
    return _dianpingArray;
}

- (NSArray *)relatedArrray{
    if (!_relatedArrray) {
        _relatedArrray = [NSArray array];
    }
    return _relatedArrray;
}

- (instancetype)initWithProduct:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        self.productDic = dic;
    }
    return self;
}

- (void)dealloc
{
    NSLog(@"销毁");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleLabel.text = [self.productDic objectForKey:@"name"]? : @"详情";
    
    self.cartButton = [[MessageButton alloc] initWithFrame:CGRectMake(kScreenWidth - 54, self.navView.height - 44, 44, 44)];
    [self.cartButton setImage:[UIImage imageNamed:@"cart"] forState:UIControlStateNormal];
    WeakSelf(weakSelf)
    [self.cartButton bk_addEventHandler:^(id sender) {
        DHShoppingCart_PushController *vc = [[DHShoppingCart_PushController alloc] init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    } forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:self.cartButton];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHProductDetail_HeadCell" bundle:nil] forCellReuseIdentifier:kDHProductDetail_HeadCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHProductDetail_DianpingCell" bundle:nil] forCellReuseIdentifier:kDHProductDetail_DianpingCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHProductDetail_ProductCell" bundle:nil] forCellReuseIdentifier:kDHProductDetail_ProductCell];
    
    if (self.productDic) {
        self.productID = [self.productDic objectForKey:@"_id"];
    }
    
    [self loadSize];
    
    if ([Login isLogin]) {
        [self loadCartNumber];
    }
    [self loadDianping];
    [self loadProduct];
    
    [self loadProductDetail];
}

- (void)loadProductDetail{
    [[DHNetAPIClient shareDHNetAPIClient] request_products_detailWithDictionary:@{@"product_id":self.productID} andBlock:^(id data, NSError *error) {
        if (data) {
            self.productDic = [data objectForKey:@"detail"];
            [self.tableView reloadData];
        }
    }];
}

- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    if ([eventName isEqualToString:@"refresh_cart_number"]) {
        [self loadCartNumber];
    }
}

- (void)loadCartNumber{
    [[DHNetAPIClient shareDHNetAPIClient] request_cart_countAndBlock:^(id data, NSError *error) {
        NSInteger count = [[data objectForKey:@"count"] integerValue];
        self.cartButton.countLabel.text = [NSString stringWithFormat:@"%ld",(long)count];
    }];
}

- (void)loadSize{
    [[DHNetAPIClient shareDHNetAPIClient] request_products_sizesWithDictionary:@{@"product_id":self.productID} andBlock:^(id data, NSError *error) {
        if (data) {
            self.sizesArray = [data objectForKey:@"sizes"];
            [self.tableView reloadData];
        }
    }];
}

// 点评
- (void)loadDianping{
    
    NSString *brandID;
    if ([self.productDic objectForKey:@"brand"]) {
        brandID = [[self.productDic objectForKey:@"brand"] objectForKey:@"_id"];
    }else{
        brandID = self.brandID;
    }
    NSDictionary *params = @{@"pageIndex":@(0),
                             @"pageSize":@(3),
                             @"brand_id":brandID
                             };
    [[DHNetAPIClient shareDHNetAPIClient] request_articles_brand_remarkWithDictionary:params andBlock:^(id data, NSError *error) {
        self.dianpingArray = [data objectForKey:@"remarks"];
        [self.tableView reloadRow:1 inSection:0 withRowAnimation:UITableViewRowAnimationNone];
    }];
}

// 相关商品
- (void)loadProduct{
    [[DHNetAPIClient shareDHNetAPIClient] request_products_relatedWithDictionary:@{@"product_id":self.productID} andBlock:^(id data, NSError *error) {
        self.relatedArrray = [data objectForKey:@"related"];
        [self.tableView reloadRow:2 inSection:0 withRowAnimation:UITableViewRowAnimationNone];
    }];
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDelegate and UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        CGFloat h = 320 + kScreenWidth * 1.29;
//        CGFloat sizeScrollViewW = self.view.width - 139;
//        CGFloat sizeAllWidth = self.sizesArray.count * 50;
//        if (sizeAllWidth > sizeScrollViewW) {
//            h += 50;
//        }
        return h;
    }else if (indexPath.row == 1){
        if (self.dianpingArray.count == 0) {
            return 0;
        }else{
            return 36 + self.dianpingArray.count * 95;
        }
    }else if (indexPath.row == 2){
        CGFloat w = (kScreenWidth - 16 - 8)/2;
        CGFloat h = w * 1.29 + 61;
        return 40 + (self.relatedArrray.count / 2) * h;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        DHProductDetail_HeadCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHProductDetail_HeadCell forIndexPath:indexPath];
        [cell updateCell:self.productDic sizes:self.sizesArray];
        return cell;
    }else if (indexPath.row == 1){
        DHProductDetail_DianpingCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHProductDetail_DianpingCell forIndexPath:indexPath];
        [cell updateCellWithArray:self.dianpingArray];
        cell.brandID = [[self.productDic objectForKey:@"brand"] objectForKey:@"_id"];
        if (self.dianpingArray.count == 0) {
            cell.contentView.hidden = YES;
        }else{
            cell.contentView.hidden = NO;
        }
        return cell;
    }else if (indexPath.row == 2){
        DHProductDetail_ProductCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHProductDetail_ProductCell forIndexPath:indexPath];
        [cell updateCellWithArray:self.relatedArrray];
        return cell;
    }
    else{
        return nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
